/*
 * PrintTableDAO.java
 *
 * Created on April 11, 2007, 12:48 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.jjenkins.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.io.InputStream;
/**
 * This method also contains the CRUD methods
 * @author Intex2Week
 */
public class PrintTableDAO
{
    private static PrintTableDAO instance = null;
    private PreparedStatement insert = null;
    /** Creates a new instance of PrintTableDAO */
    public PrintTableDAO ()
    {
    }
    
    /**
     * Gets the single instance
     * @return 
     */
    public static synchronized PrintTableDAO getInstance() {
        if (instance == null) {
            instance = new PrintTableDAO();
        }
        return instance;
     }
    
     /**
     * insert photoset
     */
    public void insert(String fname, long fsize, String ftype, InputStream fimage)throws Exception {
        Connection conn = ConnectionPool.getInstance().get();
        insert(fname, fsize, ftype, fimage, conn);
    }
    /**
     * insert photoset
     */
    public void insert(String fname, long fsize, String ftype, InputStream fimage , Connection conn)throws Exception{
        try {
            this.insert = conn.prepareStatement("insert into print_table values (?,?,?,?,?)");
            insert.setString(1,GUID.generate ());
            insert.setString(2,fname);
            insert.setLong (3,fsize);
            insert.setString (4,ftype);
            int intSize = (int)fsize; 
            insert.setBinaryStream (5, fimage, intSize);
            
            insert.execute();
            conn.commit();
            ConnectionPool.getInstance().release(conn);
        } catch (SQLException ex) {
            ex.printStackTrace();
            conn.rollback();
            ConnectionPool.getInstance().release(conn);
            throw ex;
        }
    }
    
    /**
     * return the member object
     * @param mem 
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     * @return member object
     */
    public List readByMember(MemberBO mem)throws DataException{
        
        List files;
        try{
        files = new LinkedList();
        
        // retrieve a database connection from the pool
        ConnectionPool connPool = ConnectionPool.getInstance();
        Connection conn = connPool.get();
        
        
        PreparedStatement pstmt = conn.prepareStatement("SELECT backup_id, file_name, extension_id, size_of_file FROM file_backup WHERE member_id = ?");
        pstmt.setString(1, mem.getId());
        pstmt.execute();
        ResultSet rs = pstmt.getResultSet();
        
        while(rs.next()) {
            ExtensionBO url = ExtensionDAO.getInstance().readByType(rs.getString("extension_id"),conn);
            
            String[] fileinfo = {rs.getString("backup_id"), url.getIcon(),  rs.getString("file_name"), rs.getLong("size_of_file") + ""};
            System.out.println(fileinfo);
            files.add(fileinfo);
            
        }
        conn.commit();
        connPool.release(conn);
        }catch (ConnectionPoolException e) {
            throw new DataException("Could not save record for", e);
        } catch (SQLException e) {
            throw new DataException("Could not save record for", e);
        }
        return files;
    }
    
    
}
